package com.other.test;

import org.apache.commons.lang.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

/**
 * @Description: TODO
 * @author Junwu Zheng(junwu.zheng@boco.jp)
 * @date 2013-9-4 下午12:49:47
 * @version V1.0
 */
public class HtmlUtil {

	private final static Whitelist user_content_filter = Whitelist.relaxed();
	static {
		user_content_filter.addTags("embed", "object", "param", "span", "div");
		user_content_filter.addAttributes(":all", "style", "class", "id",
				"name");
		user_content_filter.addAttributes("object", "width", "height",
				"classid", "codebase");
		user_content_filter.addAttributes("param", "name", "value");
		user_content_filter.addAttributes("embed", "src", "quality", "width",
				"height", "allowFullScreen", "allowScriptAccess", "flashvars",
				"name", "type", "pluginspage");
	}

	/**
	 * 对用户输入内容进行过滤
	 * 
	 * @param html
	 * @return
	 */
	public static String filterUserInputContent(String html) {
		// if(StringUtils.isBlank(html)) return "";
		return Jsoup.clean(html, user_content_filter);
	}

	public static String htmlDecode(String content) {
		if (content == null)
			return "";
		String html = content;
		html = html.replaceAll("&", "&amp;");
		html = html.replace("\"", "&quot;"); // "
		html = html.replace("\t", "&nbsp;&nbsp;");// 替换跳格
		html = html.replace(" ", "&nbsp;");// 替换空格
		html = html.replace("<", "&lt;");

		html = html.replaceAll(">", "&gt;");

		return html;
	}
	
	/**
     * 变成安全的编码
     * @param str
     * @return
     */
    public static String escapeHtml(String str) {
        return StringEscapeUtils.escapeHtml(str);
    }
}
